<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户id" prop="userId">
        <el-input v-model="queryParams.userId" placeholder="请输入用户id" clearable size="small" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="queryParams.phone" placeholder="请输入手机号" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['business:customer:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['business:customer:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['business:customer:remove']" >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['business:customer:export']" >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-sold-out" plain size="mini" @click="balanceAdd">手动充值</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-sold-out" plain size="mini" @click="balanceSub">手动扣款</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-sold-out" plain size="mini" :disabled="single" @click="limitCustomer">封禁账户</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="customerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="用户id" align="center" prop="userId" width="80"/>
      <el-table-column label="上级id" align="center" prop="parentUserId" width="80"/>
      <el-table-column label="用户名" align="center" prop="userName" width="100"/>
      <el-table-column label="真实姓名" align="center" prop="realName" width="100"/>
      <el-table-column label="账户余额" align="center" prop="balance" width="100"/>
      <el-table-column label="累计收益" align="center" prop="totalProfit" width="100"/>
      <el-table-column label="手机号" align="center" prop="phone" width="120"/>
      <el-table-column label="微信号" align="center" prop="wechat" width="120"/>
      <el-table-column label="身份证号" align="center" prop="idCard" width="180"/>
      <el-table-column label="支付宝账号" align="center" prop="alipay" width="120"/>
      <el-table-column label="用户级别" align="center" prop="level" :formatter="levelFormat"/>
      <el-table-column label="实名认证" align="center" prop="isAuth" :formatter="isAuthFormat"/>
      <el-table-column label="是否封禁" align="center" prop="limitStatus" :formatter="limitStatusFormat"/>
      <el-table-column label="封禁理由" align="center" prop="limitMsg" width="180"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180"/>
      <el-table-column label="修改时间" align="center" prop="updateTime" width="180"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['business:customer:edit']" >修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['business:customer:remove']" >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <!-- 添加或修改客户信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id"/>
        </el-form-item>
        <el-form-item label="上级id" prop="parentUserId">
          <el-input v-model="form.parentUserId" placeholder="请输入上级用户id"/>
        </el-form-item>
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="真实姓名" prop="realName">
          <el-input v-model="form.realName" placeholder="请输入真实姓名" />
        </el-form-item>
        <el-form-item label="账户余额" prop="balance">
          <el-input v-model="form.balance" placeholder="请输入账户余额"/>
        </el-form-item>
        <el-form-item label="累计收益" prop="totalProfit">
          <el-input v-model="form.totalProfit" placeholder="请输入累计收益"/>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号"/>
        </el-form-item>
        <el-form-item label="用户级别">
          <el-radio-group v-model="form.level">
            <el-radio v-for="dict in levelOptions" :key="dict.dictValue" :label="parseInt(dict.dictValue)" >{{ dict.dictLabel }}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--  手动充值/手动扣款对话框  -->
    <el-dialog :title="title" :visible.sync="balanceEdit" width="400px" append-to-body>
      <div slot="footer" class="dialog-footer">
        <el-form>
          <el-form-item label="用户id">
            <el-select v-model="balanceEditParam.selectValue" placeholder="请选择">
              <el-option v-for="item in customerList" :key="item.userId" :label="item.userId + '-' + item.userName" :value="item.userId"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="金额">
              <el-input-number v-model="balanceEditParam.balanceNumber" :min="1" :max="999999" label="描述文字"/>
          </el-form-item>
        </el-form>
        <el-button type="primary" @click="balanceEditSave">确 定</el-button>
        <el-button @click="balanceEditCancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--  封禁用户对话框  -->
    <el-dialog :title="title" :visible.sync="limitCustomerDialog" width="400px" append-to-body>
      <el-form ref="limitCustomerForm" :model="limitCustomerForm" :rules="limitStatusRules">
        <el-form-item>
          <el-input type="hidden" v-model="limitCustomerForm.id"/>
        </el-form-item>
        <el-form-item label="请填写封禁理由" prop="limitMsg">
          <el-input type="textarea" v-model="limitCustomerForm.limitMsg" label="请填写封禁理由"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="limitCustomerSave">确 定</el-button>
        <el-button @click="limitCustomerCancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCustomer,
  getCustomer,
  delCustomer,
  addCustomer,
  updateCustomer,
  exportCustomer,
  balanceEditCustomer,
  limitCustomer
} from "@/api/business/customer";

export default {

  name: "Customer",
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户信息表格数据
      customerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 手动充值/手动扣费
      balanceEdit : false,
      // 手动充值/扣费弹出层参数
      balanceEditParam : {
        //充值金额
        balanceNumber : 0,
        //搜索栏参数
        selectValue : '',
        //类型: 1:充值; 2:扣款
        type : ''
      },
      //封禁用户
      limitCustomerDialog : false,
      limitCustomerForm : {},

      // 用户级别字典
      levelOptions: [],
      // 是否通过实名认证字典
      isAuthOptions: [],
      // 是否通过实名认证字典
      limitStatusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        phone: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [{
          required: true,
          message: "用户id不能为空",
          trigger: "blur"
        }],
        parentUserId: [{
          required: true,
          message: "上级用户id不能为空",
          trigger: "blur"
        }],
        balance: [{
          required: true,
          message: "账户余额不能为空",
          trigger: "blur"
        }],
        totalProfit: [{
          required: true,
          message: "累计收益不能为空",
          trigger: "blur"
        }],
        phone: [{
          required: true,
          message: "手机号不能为空",
          trigger: "blur"
        }],
        level: [{
          required: true,
          message: "用户级别不能为空",
          trigger: "blur"
        }],
        isAuth: [{
          required: true,
          message: "实名认证不能为空",
          trigger: "blur"
        }]
      },
      limitStatusRules: {
        limitMsg : [{
          required: true,
          message : "封禁理由不能为空",
          trigger: "blur"
        }]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("user_level").then(response => {
      this.levelOptions = response.data;
    });
    this.getDicts("is_auth").then(response => {
      this.isAuthOptions = response.data;
    });
    this.getDicts("limit_status").then(response => {
      this.limitStatusOptions = response.data;
    });
  },
  methods: {
    /** 查询客户信息列表 */
    getList() {
      this.loading = true;
      listCustomer(this.queryParams).then(response => {
        this.customerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 用户级别字典翻译
    levelFormat(row) {
      return this.selectDictLabel(this.levelOptions, row.level);
    },
    // 是否通过实名认证字典翻译
    isAuthFormat(row) {
      return this.selectDictLabel(this.isAuthOptions, row.isAuth);
    },
    // 封禁状态翻译
    limitStatusFormat(row) {
      return this.selectDictLabel(this.limitStatusOptions, row.limitStatus);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        parentUserId: null,
        balance: null,
        totalProfit: null,
        phone: null,
        wechat: null,
        idCard: null,
        alipay: null,
        level: 0,
        isAuth: 0
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCustomer(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客户信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCustomer(this.form).then(() => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCustomer(this.form).then(() => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除客户信息编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delCustomer(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有客户信息数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return exportCustomer(queryParams);
      }).then(response => {
        this.download(response.msg);
      })
    },
    /* 手动充值*/
    balanceAdd(){
      this.balanceEdit = true;
      this.title = "手动充值";
      this.balanceEditParam.type = 1;
    },
    /*手动扣款*/
    balanceSub(){
      this.balanceEdit = true;
      this.title = "手动扣款";
      this.balanceEditParam.type = 2;
    },
    /*手动充值/扣款保存*/
    balanceEditSave(){
      balanceEditCustomer(this.balanceEditParam).then((res) => {
        if (res.code === 200){
          this.msgSuccess("保存成功");
          this.balanceEdit = false;``
          this.getList();
        }else {
          this.msgError("保存失败, 请联系管理员");
        }

      })
    },
    /*关闭充值/扣款弹出框*/
    balanceEditCancel(){
      this.balanceEdit = false;
      this.reset();
    },
    /*封禁用户*/
    limitCustomer(){
      this.limitCustomerForm.id = this.ids[0];
      this.limitCustomerDialog = true;
      this.title = "封禁用户"
    },
    /*封禁用户执行*/
    limitCustomerSave(){
      this.$refs["limitCustomerForm"].validate(valid => {
        if (valid){
          limitCustomer(this.limitCustomerForm).then((res)=>{
            if (res.code === 200){
              this.msgSuccess("封禁成功");
              this.limitCustomerDialog = false;
              this.getList();
            }else {
              this.msgError("封禁失败")
            }
          })
        }
      })
    },
    /*封禁用户取消*/
    limitCustomerCancel(){
      this.limitCustomerDialog = false;
    }
  }
};

</script>
